package com.ruoyi.web.controller.depot;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ProcuredOrders;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.depot.domain.vo.OrdersProductVO;
import com.ruoyi.procured.service.IProcuredOrdersService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.depot.domain.Receipt;
import com.ruoyi.depot.service.IReceiptService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收货单Controller
 * 
 * @author ruoyi
 * @date 2023-12-26
 */
@RestController
@RequestMapping("/depot/receipt")
public class ReceiptController extends BaseController
{
    @Autowired
    private IReceiptService receiptService;

    @Autowired
    private IProcuredOrdersService procuredOrdersService;

    /**
     * 查询收货单列表
     */
    @PreAuthorize("@ss.hasPermi('depot:receipt:list')")
    @GetMapping("/list")
    public TableDataInfo list(Receipt receipt)
    {
        startPage();
        List<Receipt> list = receiptService.selectReceiptList(receipt);
        return getDataTable(list);
    }

    /**
     * 导出收货单列表
     */
    @PreAuthorize("@ss.hasPermi('depot:receipt:export')")
    @Log(title = "导出收货单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Receipt receipt)
    {
        List<Receipt> list = receiptService.selectReceiptList(receipt);
        ExcelUtil<Receipt> util = new ExcelUtil<Receipt>(Receipt.class);
        util.exportExcel(response, list, "收货单数据");
    }

    /**
     * 收货时获取收货单详细信息
     */
    @PreAuthorize("@ss.hasPermi('depot:receipt:query')")
    @GetMapping(value = "/{receiptId}")
    public AjaxResult getInfo(@PathVariable("receiptId") String receiptId)
    {
        return success(receiptService.selectReceiptByReceiptId(receiptId));
    }

    /**
     * 点击修改按钮时获取收货单详细信息
     */
    @PreAuthorize("@ss.hasPermi('depot:receipt:query')")
    @GetMapping(value = "/update/{receiptId}")
    public AjaxResult getUpdateInfo(@PathVariable("receiptId") String receiptId)
    {
        return success(receiptService.selectUpdateReceiptByReceiptId(receiptId));
    }

    /**
     * 新增收货单
     */
    @PreAuthorize("@ss.hasPermi('depot:receipt:add')")
    @Log(title = "添加收货单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Receipt receipt)
    {
        receipt.setConsignee(getNickname());
        return toAjax(receiptService.insertReceipt(receipt));
    }


    /**
     * 批量新增收货单
     */
    @PreAuthorize("@ss.hasPermi('depot:receipt:add')")
    @Log(title = "批量添加收货单", businessType = BusinessType.INSERT)
    @PostMapping("/batchAddReceipt")
    public AjaxResult batchAddReceipt(@RequestBody List<Receipt> receiptList)
    {
        for (Receipt item : receiptList) {
            item.setConsignee(getNickname());
        }
        return toAjax(receiptService.insertReceiptsBatch(receiptList));
    }

    /**
     * 修改收货单
     */
    @PreAuthorize("@ss.hasPermi('depot:receipt:edit')")
    @Log(title = "修改收货单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Receipt receipt)
    {
        return toAjax(receiptService.updateReceipt(receipt));
    }

    /**
     * 删除收货单
     */
    @PreAuthorize("@ss.hasPermi('depot:receipt:remove')")
    @Log(title = "删除收货单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{receiptIds}")
    public AjaxResult remove(@PathVariable String[] receiptIds)
    {
        return toAjax(receiptService.deleteReceiptByReceiptIds(receiptIds));
    }

    @PreAuthorize("@ss.hasPermi('depot:receipt:list')")
    @GetMapping("/procuredOrdersTree")
    public AjaxResult procuredOrdersTree(ProcuredOrders procuredOrders){
        return success(procuredOrdersService.selectProcuredOrdersTree(procuredOrders));
    }

    @PreAuthorize("@ss.hasPermi('depot:receipt:list')")
    @GetMapping("/selectOrdersNumber/{ordersId}")
    public AjaxResult selectOrdersNumber(@PathVariable String ordersId){
        return success(procuredOrdersService.selectOrdersNumber(ordersId));
    }


    /**
     * 确认收货
     */
    @PreAuthorize("@ss.hasPermi('depot:receipt:edit')")
    @Log(title = "确认收货", businessType = BusinessType.UPDATE)
    @PutMapping("/updateReceiptConfirm")
    public AjaxResult updateReceiptConfirm(@RequestBody Receipt receipt)
    {
        return toAjax(receiptService.updateReceiptConfirm(receipt));
    }

    /**
     * 查询收货单是否可以进行收货
     */
    @PreAuthorize("@ss.hasPermi('depot:receipt:query')")
    @GetMapping("/validateBeforeReceiving")
    public AjaxResult validateBeforeReceiving(@RequestParam("receiptId") String receiptId)
    {
        return toAjax(receiptService.validateBeforeReceiving(receiptId));
    }

    /**
     * 查询库存待检量详细
     */
    @PreAuthorize("@ss.hasPermi('depot:receipt:query')")
    @GetMapping(value = "product/getInspectionQuantityDetails")
    public TableDataInfo openInspectionQuantityDialog(OrdersProductVO vo)
    {
        startPage();
        return getDataTable(receiptService.openInspectionQuantityDialog(vo));
    }
}
